package com.houtai.eduservice.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.houtai.eduservice.entity.EduCourse;
import com.houtai.eduservice.entity.EduCourseDescription;
import com.houtai.eduservice.entity.frontvo.CourseFrontVo;
import com.houtai.eduservice.entity.frontvo.CourseWebVo;
import com.houtai.eduservice.entity.vo.CourseInfoVo;
import com.houtai.eduservice.entity.vo.CoursePublishVo;
import com.houtai.eduservice.mapper.EduCourseMapper;
import com.houtai.eduservice.service.EduChapterService;
import com.houtai.eduservice.service.EduCourseDescriptionService;
import com.houtai.eduservice.service.EduCourseService;
import com.houtai.eduservice.service.EduVideoService;
import com.houtai.servicebase.exceptionhandler.DException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lin
 * @since 2022-11-20
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    //课程简介的注入
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;

    //添加课程信息
    @Override
    public Integer saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1.向课程表添加
        //做转换
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert <= 0) {
            //添加失败
            throw new DException(20001, "添加课程信息异常");
        }
        //获取课程id
        Integer id = eduCourse.getId();
        // 2.向课程描述表添加信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        //设置id
        eduCourseDescription.setId(id);
        courseDescriptionService.save(eduCourseDescription);
        return id;

    }

    //根据课程id查询课程基本信息
    @Override
    public CourseInfoVo getCourseInfo(Integer courseId) {
        //1.查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);
        //2.查询课程简介表
        EduCourseDescription eduCourseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        return courseInfoVo;
    }

    //修改课程基本信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if (update == 0) {
            throw new DException(20001, "修改失败");

        }
        //修改简介表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(eduCourseDescription);

    }

    //根据课程id课程确认信息
    @Override
    public CoursePublishVo publishCourseInfo(Integer id) {
        //调用mapper
        CoursePublishVo coursePublishVo = baseMapper.getPublishInfo(id);
        return coursePublishVo;
    }

    @Override
    public void removeCourse(Integer courseId) {
        //根据课程id删除小节
        eduVideoService.removeVideo(courseId);
        //根据课程id删除章节
        eduChapterService.removeChapter(courseId);
        //根据课程id删除描述
        courseDescriptionService.removeById(courseId);
        //根据课程id删除课程本身
        int r = baseMapper.deleteById(courseId);
        if (r == 0) {
            throw new DException(20001, "删除失败");
        }
    }

    /**
     * 条件查询，带分页查询课程
     * xst
     * @param pageParam
     * @param courseFrontVo
     * @return
     */
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) {
            //一级分类
            wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())) {
            //二级分类
            wrapper.eq("subject_id", courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) {
            //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {
            //最新
            wrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {
            //价格
            wrapper.orderByDesc("price");
        }
        baseMapper.selectPage(pageParam, wrapper);
        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        //下一页
        boolean hasPrevious = pageParam.hasPrevious();
        //上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;
    }

    //根据课程id，编写sql语句查询课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(Integer courseId){
        return baseMapper.getBaseCourseInfo(courseId);
    }
}


